#include "gui/supplierviewadddialog.h"
#include "gui/separatorwidget.h"
#include "core/suppliermodel.h"
#include "core/constants.h"

void SupplierViewAddDialog::initUiComponents() {
    QFont font;
    mainLayout = new QVBoxLayout;
    gridLayout = new QGridLayout;

    headerLabel = new QLabel("Add a Supplier");
    font = headerLabel->font();
    font.setPointSize(font.pointSize() + HEADER_UPSIZE);
    headerLabel->setFont(font);
    mainLayout->addWidget(headerLabel);

    subHeaderLabel = new QLabel("Enter your new supplier's details.");
    mainLayout->addWidget(subHeaderLabel);
    mainLayout->addWidget(new SeparatorWidget);

    nameLabel = new QLabel;
    nameEdit = new QLineEdit;

    countryLabel = new QLabel;
    countryEdit  = new QLineEdit;

    contactLabel = new QLabel;
    contactEdit = new QLineEdit;


    nameLabel->setText("Supplier Name");
    gridLayout->addWidget(nameLabel, 0, 0);
    gridLayout->addWidget(nameEdit, 0, 1);

    countryLabel->setText("Country of Origin");
    gridLayout->addWidget(countryLabel, 1, 0);
    gridLayout->addWidget(countryEdit, 1, 1);

    contactLabel->setText("Contact Number");
    gridLayout->addWidget(contactLabel, 2, 0);
    gridLayout->addWidget(contactEdit, 2, 1);

    mainLayout->addLayout(gridLayout);
    mainLayout->addStretch();

    mainLayout->addWidget(new SeparatorWidget);
    buttonBox = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
    mainLayout->addWidget(buttonBox);


    this->setLayout(mainLayout);
}

void SupplierViewAddDialog::onSaveButtonClicked()
{
    if (nameEdit->text() == "" || contactEdit->text() == ""
            || countryEdit->text() == "") {
        QMessageBox::critical(this, "Incomplete data", "Please fill in all input boxes to continue.");
        return;
    }

    // If we have not been provided a supplier
    if (!this->supplier) {
        this->supplier = new SupplierModel;
    }

    supplier->setName(nameEdit->text());
    supplier->setContactNumber(contactEdit->text());
    supplier->setCountry(countryEdit->text());

    this->accept();
}

SupplierModel * SupplierViewAddDialog::getSupplier()
{
    return this->supplier;
}

void SupplierViewAddDialog::initSignalsAndSlots()
{
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(onSaveButtonClicked()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

SupplierViewAddDialog::SupplierViewAddDialog(QWidget *parent) :
    QDialog(parent)
{
    this->setWindowTitle("Add Supplier");
    this->setMinimumSize(400, 300);
    this->initUiComponents();
    this->initSignalsAndSlots();
    this->supplier = NULL;
}
